/*
 * Created on 2006-3-24
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package cn.com.sbell.vss.csg.vap;

import org.dom4j.Element;

import cn.com.sbell.vss.VssConstants;
import cn.com.sbell.vss.VssException;
import cn.com.sbell.vss.csg.vap.bean.ImageParameterInfos;
import cn.com.sbell.vss.csg.vap.bean.ImageParameterInfos.ImageParameterInfo;
import cn.com.sbell.vss.csg.vsclient.VsClient;
import cn.com.sbell.vss.csg.vsclient.util.PropertiesHolder;

/**
 * @author liang.a.chen
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class GetVsImageParameters extends ProtectedVsCommander {

	protected Element executeCommand(VsClient vsClient, PropertiesHolder holder, Element root) {
		Integer videoId = holder.getIntProperty("videoId");
		if(videoId == null){
			throw new VssException(VssConstants.CODE_REQUIRED,new String[]{"csg.videoId"});
		}
		ImageParameterInfos paramInfos=null;
		try {
			paramInfos = vsClient.getImageParameterInfo_3N(videoId.intValue());
		} catch (VssException e) {
			logger.debug("get 3N wrong", e);
//			NET_DVR_NOSUPPORT
			if(vsClient.getLastStatusCode()==VssConstants.SERVER_NOT_SUPPORT||
					vsClient.getLastStatusCode()==VssConstants.SERVER_DATA_ERROR){
				paramInfos = vsClient.getImageParameterInfo(videoId.intValue());
			}else{
				throw e;
			}			
		}
		ImageParameterInfo[] ipi=paramInfos.getNets();
		for (int i = 0; i < ipi.length; i++) {
			parseToXML(root.addElement("net"), ipi[i]);
		}		
		parseToXML(root.addElement("rec"), paramInfos.getRec());
		
		return root;
	}
	
	private void parseToXML(Element element, ImageParameterInfo imageInfo){
		element.addElement("videoId").addText(toString(imageInfo.getVideoId()));
		element.addElement("bitRate").addText(toString(imageInfo.getBitRate()));
		element.addElement("frameRate").addText(toString(imageInfo.getFrameRate()));
		element.addElement("imageSize").addText(toString(imageInfo.getImageSize()));
		element.addElement("flag").addText(toString(imageInfo.getFlag()));
		element.addElement("bitRateType").addText(toString(imageInfo.getBitRateType()));
		element.addElement("picQuality").addText(toString(imageInfo.getPicQuality()));
		element.addElement("iFrameInterval").addText(toString(imageInfo.getIFrameInterval()));
		element.addElement("streamType").addText(toString(imageInfo.getStreamType()));
		element.addElement("subStreamId").addText(toString(imageInfo.getSubStreamId()));
	}
}
